<template>
  <el-dialog title="设置头像" :visible.sync="dialogVisible" width="600px">
    <div style="min-height: 200px">
      <UploadHeadPic v-if="dialogVisible" @e-headPicId="setHeadPic" />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleEdit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { tbUserUpdate, tbUserQueryOne } from '@/api/admin/user/user-manage.js'
import userInfo from '@/mixins/userInfo.js'
export default {
  mixins: [userInfo],
  components: {
    UploadHeadPic: () => import('@/components/uploadHeadPic/index.vue'),
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        id: '',
        attachmentId: '', //头像
      },
    }
  },
  mounted() {
    this.$bus.$on('showAvatarSetting', () => {
      this.dialogVisible = true
    })
  },
  beforeDestroy() {
    this.$bus.$off('showAvatarSetting')
  },
  methods: {
    setHeadPic(v) {
      this.form.attachmentId = v
    },
    handleEdit() {
      this.form.id = this.$store.getters.userId
      if (!this.form.attachmentId) {
        this.warnMsg('请上传头像')
        return
      }
      tbUserUpdate(this.form).then((res) => {
        this.dialogVisible = false
        tbUserQueryOne(this.form.id).then((res) => {
          let data = res.data.data[0]
          let userinfo = JSON.parse(this.getSession('userinfo'))
          userinfo.sysAttachment.httpUrl = data.sysAttachment.httpUrl
          this.setSession('userinfo', JSON.stringify(userinfo))
          this.setUserInfo()
        })
      })
    },
  },
}
</script>
<style scoped lang="scss">
</style>